package com.git.mail;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServlet;

import com.git.util.PropLoader;

/**
 * Servlet implementation class BrInitServlet
 */
	public class BrInitServlet extends HttpServlet {
    
	/**
	 * 
	 */
	private static final long serialVersionUID = 7262790048592259831L;
	public static EmailAutherticator mailAutherticator = null;
	public static String mailSender = null;
	public static String mailSmtpHost = null;
	public static String appRealPathBase = null;
	public static String serverAddress = null;
	public static String mailCc = null;
	public static int pageRpp = 20;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BrInitServlet() {
		final Properties prop = new PropLoader("config.properties").getProp();
		
		mailSender = prop.getProperty("mail.sender");
		mailSmtpHost = prop.getProperty("mail.smtp.host");
		if ("true".equals(prop.getProperty("mail.smtp.auth")))
		{
			mailAutherticator = 
				new EmailAutherticator(prop.getProperty("mail.smtp.auth.username")
					, prop.getProperty("mail.smtp.auth.password"));
		}
		
		String val = prop.getProperty("page.rpp"); // rows per page
		if (null != val)
			pageRpp = Integer.parseInt(val);
    }
    
	public static void sendMail(String[] vRecipient, String[] vCc, String subject, String content)
	{
		
		try
		{
			BrInitServlet  bs = new BrInitServlet();
			java.util.Properties props = new java.util.Properties();
			props.setProperty("mail.smtp.host", BrInitServlet.mailSmtpHost);
			if (null != BrInitServlet.mailAutherticator)
				props.setProperty("mail.smtp.auth", "true");
			
			Session session = Session.getInstance(props, BrInitServlet.mailAutherticator);
			session.setDebug(true);

			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(BrInitServlet.mailSender));
			Address[] vRecipientAddr = constructInetAddr(vRecipient);
			msg.setRecipients(Message.RecipientType.TO, vRecipientAddr);
			if (null != vCc)
			{
				Address[] vCcAddr = constructInetAddr(vCc);
				msg.setRecipients(Message.RecipientType.CC, vCcAddr);
			}
			
			if (null != BrInitServlet.mailCc)
				msg.addRecipient(Message.RecipientType.CC, new InternetAddress(BrInitServlet.mailCc));
			
			msg.setSubject(subject);
			msg.setSentDate(new java.util.Date());
			
			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.setContent(content.toString(), "text/html; charset=GBK");
			
			Multipart mp = new MimeMultipart();
			mp.addBodyPart(mbp1);
			msg.setContent(mp);
			
			Transport.send(msg);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static Address[] constructInetAddr(String[] vAddrStr)
			throws AddressException
		{
			Address[] v = new Address[vAddrStr.length];
			for (int i = 0; i < v.length; ++i)
			{
				v[i] = new InternetAddress(vAddrStr[i]);
			}
			
			return v;
		}
}

class EmailAutherticator extends Authenticator
{
	private String userName = null;

	private String password = null;

	public EmailAutherticator(String userName, String password)
	{
		super();
		this.userName = userName;
		this.password = password;
	}

	public PasswordAuthentication getPasswordAuthentication()
	{
		return new PasswordAuthentication(userName, password);
	}
}
